package com.hankarun.gevrek;

import java.io.UnsupportedEncodingException;

import org.apache.james.mime4j.codec.DecodeMonitor;
import org.apache.james.mime4j.codec.DecoderUtil;

import com.hankarun.gevrek.lib.ServerManager;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;

public class ReadMessage extends Activity {
	protected ServerManager app;
	
	private TextView fromview;
	private TextView dateview;
	private TextView subview;
	private TextView bodyview;
	
	private String decodeString(String s){
		if(s == null)
			return null;
		if (s.indexOf("=?") != -1) {
			return DecoderUtil.decodeEncodedWords(s,DecodeMonitor.SILENT);
		}
		try{
			return new String(s.getBytes("ISO8859-9"), "ISO8859-9");
		} catch (UnsupportedEncodingException e) {

		}
		return s;        		
	}
	
	private void update(){
        dateview.setText(app.carticle.article.getDate());
        subview.setText(decodeString(app.carticle.article.getSubject().toString()));
        fromview.setText(app.carticle.parsedFrom);
        ServerTask task = new ServerTask();
    	task.execute();
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_message);
        
        fromview = (TextView) findViewById(R.id.fromView);
        dateview = (TextView) findViewById(R.id.dateView);
        subview = (TextView) findViewById(R.id.subview);
        bodyview = (TextView) findViewById(R.id.bodyView);
        
        app = (ServerManager)getApplication();
        update();
        
        Button replubutton = (Button) findViewById(R.id.button2);
        replubutton.setOnClickListener(new Button.OnClickListener() { 
        	public void onClick (View v){ 
        		Intent myIntent = new Intent(ReadMessage.this, PostActivity.class);
        		myIntent.putExtra("reply","0");
        		ReadMessage.this.startActivityForResult(myIntent, 0);
    		}
    	});

    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==1){       	
        	setResult(1);
        	finish();
        }
    }

	 private class ServerTask extends AsyncTask<Void, Void, String> {
	    	private ProgressDialog dialog;
	    	@Override
	    	protected void onPreExecute(){
	    		dialog = ProgressDialog.show(ReadMessage.this, "", 
	    				getString(R.string.messagedownloadin), true);
	    	}
	    	
	    	@Override
	    	protected void onPostExecute(String res) {
	    		bodyview.setText(decodeString(res));
	    	    dialog.dismiss();
	    	}

	    	@Override
	    	protected String doInBackground(Void... params) {	        	    		
				return app.getBody(app.carticle.article.getArticleId());
	    	}

	    }

}
